﻿using System;

namespace WXY.Course.Common.MVCExtension
{
    public class PageInfo
    {
        private int pageNumber = 1;
        private int pageSize = 10;
        private int recordCount;

        private PageInfo(int pageNumber, int pageSize)
        {
            if (pageNumber > 0) this.pageNumber = pageNumber;
            if (pageSize > 0) this.pageSize = pageSize;
        }

        public int PageNumber
        {
            get { return pageNumber; }
            private set { pageNumber = value; }
        }

        public int PageSize
        {
            get { return pageSize; }
            private set { pageSize = value; }
        }

        /// <summary>
        /// 起始记录，从1开始
        /// </summary>
        public int StartRecord
        {
            get { return (pageNumber - 1) * pageSize + 1; }
        }

        /// <summary>
        /// 结束记录
        /// </summary>
        public int EndRecord
        {
            get { return pageNumber * pageSize; }
        }

        public int RecordCount
        {
            get
            {
                return recordCount;
            }
            set
            {
                //页码不应该超过最大页码
                int lastPage = (int)Math.Ceiling((double)value / pageSize);
                if (pageNumber > lastPage) pageNumber = lastPage;
                recordCount = value;
            }
        }

        /// <summary>
        /// 使用默认页信息，页码：1，页容量：10
        /// </summary>
        /// <returns></returns>
        public static PageInfo Default()
        {
            return Create(1, 10);
        }

        public static PageInfo Create(int? pageNumber, int pagesize)
        {
            var pageNumberValue = 1;
            if (pageNumber.HasValue) pageNumberValue = pageNumber.Value;
            return new PageInfo(pageNumberValue, pagesize);
        }
    }
}